/**
 * 
 */
package com.atozsoft.commonutil;

import com.eos.system.annotation.Bizlet;

/**
 * @author houfy
 * @date 2015-01-28 14:34:10
 * @describe 设置消息模板
 */
@Bizlet("")
public class SysTemplate {
	/**
	 * 设置模板
	 * @param paramStr 模板
	 * @param roundup  摘要字符串
	 * @return
	 */
	@Bizlet("")
	public static String  setSysTemplateParam(String paramStr,String roundup) {
		if("".equals(avoidNull(paramStr))){
			return "未定义模板";
		}
		if( "".equals(avoidNull(roundup))){
			return paramStr;
		}

		if(roundup.indexOf(";") > 0){                 //参数含有多个则分组
			String[] roundupList = roundup.split(";");
			for(String roundupInfo : roundupList){
				int n = roundupInfo.indexOf("-");      //获取参数的名称和值
				String roundupKey = roundupInfo.substring(0,n);
				String roundupValue = roundupInfo.substring(n+1);
				paramStr = replaceFun(paramStr,"#" + roundupKey +"#",roundupValue);
			}
		}
		else {     //参数只有一个
			int n = roundup.indexOf("-");      //获取参数的名称和值
			String roundupKey = roundup.substring(0,n);
			String roundupValue = roundup.substring(n+1);
			paramStr = replaceFun(paramStr,"#" + roundupKey +"#",roundupValue);
		}

		return paramStr;
	}

	/**
	 * @param strSource 要替换的字符串
	 * @param strFrom  要替换的字符
	 * @param strTo 要替换成的目标字符
	 * @return
	 */
	public static String replaceFun(String strSource, String strFrom, String strTo) {    
		if (strSource == null) {        
			return null;    
		}  
		int i = 0;
		if ((i = strSource.indexOf(strFrom, i)) >= 0) {
			char[] cSrc = strSource.toCharArray();
			char[] cTo = strTo.toCharArray();
			int len = strFrom.length();  
			StringBuffer buf = new StringBuffer(cSrc.length);  
			buf.append(cSrc, 0, i).append(cTo);
			i += len;    
			int j = i;       
			while ((i = strSource.indexOf(strFrom, i)) > 0) {  
				buf.append(cSrc, j, i - j).append(cTo);   
				i += len;  
				j = i;        
			}        
			buf.append(cSrc, j, cSrc.length - j); 
			return buf.toString(); 
		} 
		return strSource;
	}

	/**
	 * 判断是否为空
	 * @param str
	 * @return
	 */
	public static String avoidNull(Object str){
		if( str == null || "".equals(str.toString().trim()) || "null".equals(str.toString().trim()))
			return "";
		return str.toString();
	}
}
